##  File name: jlc_fig1.R

##  Author: Amanda Savage

##  Creation Date: 11/07/2023

##  Last Updated: 05/01/2024

##  Purpose: Making figure 1 for JLC manuscript



##########################
####### BEGIN CODE #######
##########################
# Load necessary libraries
library(plyr)
library(tidyverse)
library(tm)
library(lubridate)
library(ggplot2)
library(dplyr)
library(readr)
library(tidytext)
library(stringr)
library(scales)
library(tidytext)
library(foreign)
library(httr)
library(curl)
library(reshape2)
library(wordcloud)
library(knitr)
library(DT)
library(readstata13)
library(RColorBrewer)
library(ggrepel)
library(haven)
library(SnowballC)
library(gridExtra)
library(grid)
library(lattice)
library(tibble)
library(hrbrthemes)
library(egg)
library(textdata)
library(patchwork)
library(dplyr)


# Read in data
all <- read.dta13("jlc_allsentiment.dta")

# Create workable partisanship var
all <- all %>% 
  mutate(partisanship_2 = ifelse(partisanship == 1, "Democrat", "Republican")) 

# Ensure using only original data
all <- all %>% filter(new_tweet != 1)

# Subset by nominee
bmk <- subset(all, nominee == "bmk")
acb <- subset(all, nominee == "acb")
kbj <- subset(all, nominee == "kbj")




###############################################################
### FIGURE 1: MIRRORED DENSITY PLOTS OF PARTISAN SENTIMENT #### 
###############################################################





# Function to create and save plots for each dataset and event
create_plots <- function(dataset_name, dataset, event) {
  
  print(paste("Processing", event, "for", dataset_name))
  
  # Filter data by event and partisanship
  tweetSum_full_d <- dataset %>%
    filter(partisanship_2 == "Democrat" & event == !!event)
  
  tweetSum_full_r <- dataset %>%
    filter(partisanship_2 == "Republican" & event == !!event)
  
  # Calculate means
  d_p <- mean(tweetSum_full_d$positive_sentiment, na.rm = TRUE)
  r_p <- mean(tweetSum_full_r$positive_sentiment, na.rm = TRUE)
  d_n <- mean(tweetSum_full_d$negative_sentiment, na.rm = TRUE)
  r_n <- mean(tweetSum_full_r$negative_sentiment, na.rm = TRUE)
  
  
  # Create plot
  p <- ggplot() +
    ggtitle(paste("Figure1 -", dataset_name, "-", event)) +
    theme_bw() +
    theme(axis.text.y = element_blank(),
          axis.ticks.y = element_blank(),
          plot.title = element_text(size = 9)) +
    geom_density(data = tweetSum_full_d, aes(x = positive_sentiment, y = after_stat(density)), fill = "blue", linetype = "blank", alpha = 0.7) +
    geom_density(data = tweetSum_full_r, aes(x = positive_sentiment, y = after_stat(density)), fill = "red", linetype = "blank", alpha = 0.7) +
    geom_segment(aes(x = r_p, y = 0, xend = r_p, yend = .3), color = "darkred", linetype = "solid", size = .75) +
    geom_segment(aes(x = d_p, y = 0, xend = d_p, yend = .3), color = "darkblue", linetype = "dashed", size = .75) +
    geom_density(data = tweetSum_full_d, aes(x = negative_sentiment, y = -after_stat(density)), fill = "blue", linetype = "blank", alpha = 0.7) +
    geom_density(data = tweetSum_full_r, aes(x = negative_sentiment, y = -after_stat(density)), fill = "red", linetype = "blank", alpha = 0.7) +
    geom_segment(aes(x = r_n, y = 0, xend = r_n, yend = -.3), color = "darkred", linetype = "solid", size = .75) +
    geom_segment(aes(x = d_n, y = 0, xend = d_n, yend = -.3), color = "darkblue", linetype = "dashed", size = .75) +
    scale_x_continuous(limits = c(-10, 10)) +
    xlab("Tweet Sentiment") +
    ylab("")
  
  # Save plot as PDF
  pdf(paste("Figure1_", dataset_name, "_", event, ".pdf", sep = ""))
  print(p)
  dev.off()
}


# List of datasets
datasets <- list(bmk = bmk, acb = acb, kbj = kbj)


# Loop through datasets and events
for (dataset_name in names(datasets)) {
  dataset <- datasets[[dataset_name]]
  unique_events <- unique(dataset$event)
  

  for (event in unique_events) {
    create_plots(dataset_name, dataset, event)
  }
}

